package top.oneit.jdownload.utils;

import java.text.DecimalFormat;

/**
 * @author lingkang
 * Created by 2021/12/9
 */
public class StorageUtils {

    /**
     * 1025 --> 1.00 KB
     */
    public static String byteToString(Number bytes) {
        Integer num = (Integer) bytes;
        if (num < 1024) {
            return num + " B";
        }
        DecimalFormat df = new DecimalFormat("#.##");
        Float si = num / 1024f;
        if (si < 1024)
            return df.format(si) + " KB";
        else if ((si = si / 1024) < 1024) {
            return df.format(si) + " MB";
        }
        si = si / 1024;
        return df.format(si) + " GB";
    }

    public static String byteToSpeedString(Number bytes) {
        Integer num = (Integer) bytes;
        if (num < 1024) {
            return num + " b/s";
        }
        DecimalFormat df = new DecimalFormat("#.##");
        Float si = num / 1024f;
        if (si < 1024)
            return df.format(si) + " kb/s";
        else if ((si = si / 1024) < 1024) {
            return df.format(si) + " mb/s";
        }
        si = si / 1024;
        return df.format(si) + " gb/s";
    }
}
